﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MeTalker.Aide.Forms
{
    internal class MeTabPage : System.Windows.Forms.TabPage
    {
        private System.Windows.Forms.TreeView treeView;
        public event TreeViewEventHandler TreeViewEvent;
        public event TreeViewCancelEventHandler TreeViewCancelEvent;
        public event TreeNodeMouseClickEventHandler TreeNodeMouseClickEvent;
        public event TreeNodeMouseClickEventHandler TreeNodeMouseDoubleClickEvent;
        public event MouseEventHandler BlankMouseClickEvent;
        bool isExpand = false;
        public bool IsFileNode
        {
            get;
            set;
        }
        public MeTabPage()
        {
            InitializeComponent();
            IsFileNode = true;
            treeView.TabIndex = 10;
            //this.treeView.Click += new EventHandler(treeView_Click);
        }


        internal void ExpandCollapseAll()
        {
            if (treeView != null)
            {
                if (isExpand)
                {
                    treeView.CollapseAll();
                    isExpand = false;
                }
                else
                {
                    treeView.ExpandAll();
                    isExpand = true;
                }
                
            }
        }

        void MeTabPage_MouseEnter(object sender, EventArgs e)
        {
            MeTabControlEx meTab = this.Parent as MeTabControlEx;
            if (meTab != null)
            {
                meTab.SelectedTab = this;
            }
        }

        private void InitializeComponent()
        {
            this.treeView = new System.Windows.Forms.TreeView();
            this.SuspendLayout();
            // 
            // treeView
            // 
            this.treeView.Dock = System.Windows.Forms.DockStyle.Fill;
            this.treeView.ItemHeight = 22;
            this.treeView.LineColor = System.Drawing.Color.Empty;
            this.treeView.Location = new System.Drawing.Point(3, 3);
            this.treeView.Name = "treeView";
            this.treeView.ShowNodeToolTips = true;
            this.treeView.Size = new System.Drawing.Size(356, 417);
            this.treeView.TabIndex = 0;
            this.treeView.BeforeSelect += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView_BeforeSelect);
            this.treeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView_AfterSelect);
            this.treeView.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeView_NodeMouseClick);
            this.treeView.NodeMouseDoubleClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeView_NodeMouseDoubleClick);
            this.treeView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView_MouseDown);
            this.treeView.MouseLeave += new System.EventHandler(this.treeView_MouseLeave);
            // 
            // MeTabPage
            // 
            this.Controls.Add(this.treeView);
            this.ResumeLayout(false);

        }

        void treeView_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                TreeNode tn = this.treeView.GetNodeAt(e.X, e.Y);
                if (tn == null)
                {
                    if (BlankMouseClickEvent != null)
                        BlankMouseClickEvent(sender, e);
                }
            }
        }
        
        void treeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                return;
            }
            if (IsFileNode)
            {
                if (e.Node != null && e.Node.Nodes != null && e.Node.Nodes.Count == 0)
                {
                    if (TreeNodeMouseDoubleClickEvent != null)
                        TreeNodeMouseDoubleClickEvent(sender, e);
                }
            }
            else
            {
                if (TreeNodeMouseDoubleClickEvent != null)
                    TreeNodeMouseDoubleClickEvent(sender, e);
            }
        }

        void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (IsFileNode)
            {
                if (e.Node != null && e.Node.Nodes != null && e.Node.Nodes.Count == 0)
                {
                    if (TreeViewEvent != null)
                        TreeViewEvent(sender, e);
                }
            }
            else
            {
                if (TreeViewEvent != null)
                    TreeViewEvent(sender, e);
            }
        }

        private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right) {
                if (e.Node != null && e.Node.Nodes != null && e.Node.Nodes.Count == 0)
                {
                    MeTalker.Aide.data.Node myNode = e.Node.Tag as MeTalker.Aide.data.Node;
                    if (myNode != null)
                    {
                        frmTypeEdit edit = new frmTypeEdit(e.Node.Text, myNode);
                        DialogResult result = edit.ShowDialog();
                        if (result == DialogResult.Yes)
                        {
                            if (!string.IsNullOrEmpty(edit.MessageInfo))
                            {
                                e.Node.Text = edit.MessageInfo;
                                myNode.Text = edit.MessageInfo;
                            }
                        }
                    }
                }
                return;
            }

            if (IsFileNode)
            {
                if (e.Node != null && e.Node.Nodes != null && e.Node.Nodes.Count == 0)
                {
                    if (TreeNodeMouseClickEvent != null)
                        TreeNodeMouseClickEvent(sender, e);
                    return;
                }
            }
            else
            {
                if (TreeNodeMouseClickEvent != null)
                    TreeNodeMouseClickEvent(sender, e);
            }
        }
        void treeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (IsFileNode)
            {
                if (e.Node != null && e.Node.Nodes != null && e.Node.Nodes.Count == 0)
                {
                    if (TreeViewCancelEvent != null)
                        TreeViewCancelEvent(sender, e);
                    return;
                }
            }
            else
            {
                if (TreeViewCancelEvent != null)
                    TreeViewCancelEvent(sender, e);
            }
        }


        internal TreeView TreeViewData
        {
            get { return treeView; }
        }

        private void treeView_MouseLeave(object sender, EventArgs e)
        {
            //if (this.CanFocus) {
            //    this.Focus();
            //}
        }   
    }
}